/** @file sa2dRunner.cpp
    @brief Example of a main using the XML parser and the call to the simulator. 

 	The parser reads and XML file. The file can be passed in the command line
    @verbatim > ./simulator -f <XML file without extension> @endverbatim
 
 	test.xml is loaded in case no file is given. 
 
    @author Andri Lareida
    
    @date 20. August, 2009
    

*/

/** @example ExampleTest.xml
    Here is an example of the XML file 
*/    

/**	@brief Inludes the simulator*/
#include "sa2d.h" 

/**	@brief Inludes the XML parser*/
#include "DomBuilder.h" 

/**	@brief Container for the objects in the simulation. 
 See (http://code.google.com/p/chipmunk-physics/)*/
cpSpace *space; 

/**	@brief Container for the static objects in the simulation. 
 See (http://code.google.com/p/chipmunk-physics/)*/
cpBody *staticBody;

/**	@brief Pointer to file to store information to restart a simulation. */
FILE *restart;

/**	@brief Time to write a restart file. */	  
double restart_time;
	  
/**	@brief global numbering of the actor in set i element j */
int** actorIndex;

/**	@brief Pointer to file to store information about the simulation. */	  
FILE **output;
	  
/**	@brief Time to write a restart file. */	  
int output_time;

/**	@brief Time step of the simulation. */	  	  
cpFloat dt = 1.0e-2;

/**	@brief Ticks counter for the render
	@todo fps control should be implemented */	  	  
int ticks=0;


/**	@brief Fills in the data from the XML and runs the simulation. 
	@include ExampleSa2dRunner.cpp
*/
	  
int main(int argc, const char **argv) {


	// Reading options form the argument list ******
	char* configfile = "";
	int fflag = 0;
	int c;
	opterr = 0;

	while ((c = getopt (argc, (char **)argv, "f:r:p:")) != -1)
	    switch (c)
        {
          case 'f': // File with configuration
           fflag = 1;
		   configfile=optarg;
        }
	if(!strcmp(configfile,"")){
		configfile="test";
	}
	// **********
	
/*	Creation of the simulator object. */	  	  
	sa2d * simulator = new sa2d();
	
/*	Creation of the XML Parser object. */	  	  	
	DomBuilder* builder = new DomBuilder();
	
	configurationfile* configuration = builder->createDom(configfile);

/*	Output the configuration to std output. */	  	  	
	cout<<configuration->toString(0)<<"\n";
/*	Runs the simulation. */	  	  	
	simulator->runSim(argc, argv, configuration);

	return 0;
}
